#include "sence.h"
#include "ui_sence.h"
#include <QHostAddress>
#include <QByteArray>
#include <QBuffer>
#include <QImage>
#include <QPixmap>
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    udpSocket=new QUdpSocket(this);
    QHostAddress address("192.168.136.1");
     quint16 port = 50538;  // 指定固定端口号
    if (udpSocket->bind(address, port, QUdpSocket::ShareAddress)) {
        qDebug() << "Socket successfully bound to" << address.toString() << "port" << port;
    } else {
        qDebug() << "Failed to bind socket to" << address.toString() << "port" << port;
        qDebug() << "Error:" << udpSocket->errorString();
    }

    connect(ui->sendButton,SIGNAL(clicked()),
            this,SLOT(sendButton_clicked()));
    connect(udpSocket,SIGNAL(readyRead()),
            this,SLOT(readPendingDatagrams()));
    connect(ui->buzhuoButton,SIGNAL(clicked()),
            this,SLOT(buzhuoButton_clicked()));
    connect(ui->previousButton,SIGNAL(clicked()),
            this,SLOT(showPreviousImage()));
    connect(ui->nextButton,SIGNAL(clicked()),
            this,SLOT(showNextImage()));
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::updateImageDisplay()
{
    if (currentImageIndex >= 0 && currentImageIndex < imagePaths.size()) {
           QPixmap savedPixmap(imagePaths[currentImageIndex]);
           if (!savedPixmap.isNull()) {
               ui->savedImageLabel->setPixmap(savedPixmap);
               ui->savedImageLabel->resize(savedPixmap.size());
               qDebug() << "Image displayed on savedImageLabel";
           } else {
               qDebug() << "Failed to load saved image";
           }
       }
}
void Dialog::sendButton_clicked()
{
    QString message=ui->messageLineEdit->text();
    QByteArray data = message.toUtf8();
    udpSocket->writeDatagram(data, QHostAddress("192.168.136.128"), 8080);

}

void Dialog::readPendingDatagrams()
{
    while(udpSocket->hasPendingDatagrams()){
     //   while(1){
        QByteArray datagram;
        datagram.resize(udpSocket->pendingDatagramSize());
        udpSocket->readDatagram(datagram.data(), datagram.size());
       // QString receivedMessage = QString::fromUtf8(datagram);
       // ui->receivedMessagesTextEdit->append(receivedMessage);
         //qDebug() <<datagram;
        QImage image;
              if (image.loadFromData(datagram)) {

                  ui->imageLabel->setPixmap(QPixmap::fromImage(image));
                  ui->imageLabel->resize(image.size());
              }
              else {
                      qDebug() << "Failed to load image from data";
                   }
    }
}
void Dialog::buzhuoButton_clicked()
{
    qDebug()<<"65";
    QPixmap pixmap = *ui->imageLabel->pixmap();
    QString fileName = QFileDialog::getSaveFileName(this, "Save Image", "", "PNG Files (*.png);;JPEG Files (*.jpg)");
    if (!fileName.isEmpty()) {
       if (pixmap.save(fileName)) {
           imagePaths.append(fileName);
           currentImageIndex = imagePaths.size() - 1;
            updateImageDisplay();
       }


         }
}

void Dialog::showPreviousImage()
{
    if (currentImageIndex > 0) {
           currentImageIndex--;
           updateImageDisplay();
       }
}

void Dialog::showNextImage()
{
    if (currentImageIndex < imagePaths.size() - 1) {
          currentImageIndex++;
          updateImageDisplay();
      }
}
